package com.zho.common.velocity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Map;

import com.zho.util.Logit;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

public class VelocityTemplate {
    private static Logit log = Logit.getInstance(VelocityTemplate.class);

    private static Cache cache;
    
    private static Map<String, String> mapTmplNames;

    /**
     * sets cache name to be used
     */
    public void setCache(Cache cache) {
        VelocityTemplate.cache = cache;
    }

    /** ************************************************************************ */
    // Cache for Velocity template - Using EHCache
    /** ************************************************************************ */

    /**
     * 
     * @param filePath
     * @return
     */
    public static StringReader getTemplateContent(String filePath, boolean usingCache) {
        filePath = (filePath == null ? "" : filePath.trim());
        if (filePath.length() > 0) {
            String strContent = null;
            if (usingCache) {
                String cacheKey = String.valueOf(filePath.hashCode());
                Element element = VelocityTemplate.cache.get(cacheKey);
                if (element == null) {
                    strContent = getFileContent(new File(filePath));
                    if (strContent != null && strContent.length() > 0) {
                        element = new Element(cacheKey, strContent);
                        cache.put(element);
                    } else {
                        return null;
                    }
                } else {
                    strContent = (String) element.getValue();
                }
            } else {
                strContent = getFileContent(new File(filePath));
            }
            return (strContent == null ? null : new StringReader(strContent));
        }
        return null;
    }

    /**
     * 
     * @param filePath
     * @return
     */
    private static String getFileContent(File file) {
        FileInputStream fin = null;
        InputStreamReader in = null;
        BufferedReader rdr = null;
        try {
            int i;
            int _MB = 1024; // 1M
            StringBuffer str = new StringBuffer();
            char[] charTmp = new char[_MB];
            fin = new FileInputStream(file);
            in = new InputStreamReader(fin, "UTF-8");
            rdr = new BufferedReader(in);
            while ((i = rdr.read(charTmp, 0, _MB)) != -1) {
                str.append(new String(charTmp, 0, i));
            }
            return str.toString();
        } catch (Exception e) {
            log.error("getFileContent()::", e);
            return null;
        } finally {
            try {
                if (rdr != null) {
                    rdr.close();
                }
            } catch (Exception e) {
            }

            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
            }

            try {
                if (fin != null) {
                    fin.close();
                }
            } catch (Exception e) {
            }
        }
    }

	/**
	 * @return the mapTmplNames
	 */
	public static Map<String, String> getMapTmplNames() {
		return mapTmplNames;
	}

	/**
	 * @param mapTmplNames the mapTmplNames to set
	 */
	public void setMapTmplNames(Map<String, String> _mapTmplNames) {
		mapTmplNames = _mapTmplNames;
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	public static String getTempName(String key) {
		return (mapTmplNames == null ? "" : mapTmplNames.get(key));
	}
}
